//
//  STShoppingController.m
//  CuiBoStore
//
//  Created by 崔博 on 16/11/28.
//  Copyright © 2016年 崔博. All rights reserved.
//


#import "STShoppingController.h"
#import "STBuyCarHandle.h"
#import "STBuyCarListCell.h"
#import "STPriceView.h"
#import "STBuyCarListModel.h"
#import "STSureOrderController.h"
#import "STSureOrderController.h"
@interface STShoppingController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *buyCarTableView;
@property (nonatomic, strong) NSMutableArray *dataList;
@property (nonatomic, strong) STPriceView *priceView;
@end

@implementation STShoppingController

static NSString *cellID = @"STBuyCarListCell";

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view addSubview:self.buyCarTableView];
    [self.view addSubview:self.priceView];
    [_buyCarTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        MASONRYINSERT(self.view, 0, 0, 0, 0);
    }];
    [_priceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(@(-50));
        make.height.equalTo(@55);
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self loadData];
}
#pragma mark - methoed
- (void)loadData
{
    [STBuyCarHandle excuteBuyCarListSucess:^(id obj) {
        NSArray *array = obj;
        if (array.count > 0) {
            // 有数据
            self.dataList = [NSMutableArray arrayWithArray:obj];
            [self.buyCarTableView reloadData];
            [self returnMoney];
        }else
        {
            // 没有数据
        }
    } faile:^(id obj) {
        
    }];
}
- (double)returnMoney
{
    double money;
    for (STBuyCarListModel *m in self.dataList) {
        if (m.isSelectButton) {
            money += m.GoodsCount * m.Price;
        }
    }
    self.priceView.price = [NSString stringWithFormat:@"%.2f",money];
    return money;
}
- (void)updataBuyCar:(STBuyCarListModel *)m type:(NSString *)type
{
    NSString * buyCarData;
    NSMutableArray *buyCarDataArray = [NSMutableArray arrayWithCapacity:0];
    for (STBuyCarListModel *model in self.dataList) {
        buyCarData = [NSString stringWithFormat:@"%@,%li",model.UUID,model.GoodsCount];
        [buyCarDataArray addObject:buyCarData];
    }
    
    buyCarData = [buyCarDataArray componentsJoinedByString:@"#"];
    [STBuyCarHandle excuteDeleteBuyCarUpdateCartMsg:buyCarData sucess:^(id obj) {
        [SVProgressHUD showSuccessWithStatus:obj];
        if ([type isEqualToString:@"+"]) {
            m.GoodsCount = m.GoodsCount + 1;
        }else
        {
            m.GoodsCount = m.GoodsCount - 1;
        }
        [self.buyCarTableView reloadData];
        [self returnMoney];
    } faile:^(id obj) {
        
    }];
}
- (void)payType{
    [STBuyCarHandle excutePayMoneyTypeProductInfor:[self gotopay] sucess:^(id obj) {
        
        STSureOrderController *order = [[STSureOrderController alloc] init];
        order.sureOrderModel = obj;
        [self.navigationController pushViewController:order animated:YES];
        
    } faile:^(id obj) {
        
    }];
}
- (NSDictionary *)gotopay
{
    NSString * buyCarData;
    NSMutableArray *buyCarDataArray = [NSMutableArray arrayWithCapacity:0];
    for (STBuyCarListModel *model in self.dataList) {
        buyCarData = [NSString stringWithFormat:@"%ld,%@,%lf",(long)model.GoodsCount,model.GoodsId,model.Weight];
        [buyCarDataArray addObject:buyCarData];
    }
    buyCarData = [buyCarDataArray componentsJoinedByString:@"#"];
    return @{@"Goods":buyCarData};
}
#pragma mark - delegate
#pragma mark tabledelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    STBuyCarListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    @weakify(self)
    cell.addBlock = ^(NSInteger index)
    {
        STBuyCarListModel *m = weak_self.dataList[index];
        [weak_self updataBuyCar:m type:@"+"];
    };
    
    cell.reduceBlock = ^(NSInteger index)
    {
        STBuyCarListModel *m = weak_self.dataList[index];
       [weak_self updataBuyCar:m type:@"-"];
    };
    
    cell.seletedBlock = ^(NSInteger index)
    {
        STBuyCarListModel *m = weak_self.dataList[index];
        m.isSelectButton = !m.isSelectButton;
        [weak_self.buyCarTableView reloadData];
        [self returnMoney];
    };
    
    cell.tag = 1000 + indexPath.row;
    cell.buyCarModel = self.dataList[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
        STBuyCarListModel *m = _dataList[indexPath.row];
        m.GoodsCount = -1;
        [self returnMoney];
        [self.dataList removeObjectAtIndex:indexPath.row];
        [tableView deleteRow:indexPath.row inSection:0 withRowAnimation:UITableViewRowAnimationTop];
        
    }];
    return @[rowAction];
}
#pragma mark - lazy
- (UITableView *)buyCarTableView
{
    if (!_buyCarTableView) {
        _buyCarTableView = [STUIKitTools tableDelegate:self dataSource:self nibCell:@"STBuyCarListCell" ider:cellID];
        _buyCarTableView.estimatedRowHeight = 100;
        _buyCarTableView.rowHeight = UITableViewAutomaticDimension;
    }
    return _buyCarTableView;
}
- (STPriceView *)priceView
{
    if (!_priceView) {
        _priceView = [[STPriceView alloc] init];
        @weakify(self)
        _priceView.block = ^(){
            [weak_self payType];
        };
    }
    return _priceView;
}

- (NSMutableArray *)dataList
{
    if (!_dataList) {
        _dataList = [NSMutableArray array];
    }
    return _dataList;
}
@end
